package org.shop.controller.liveManager;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.entity.Question;
import org.shop.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LiveManagerQuestionController {

private static Logger logger = LogManager.getLogger(LiveManagerQuestionController.class);
	
	@Autowired
	private QuestionService questionService;


	/**
	 * 获取问答信息列表
	 * @param req
	 * @return
	 */
	@RequestMapping("liveManager/getQuestionInfoList")
	@ResponseBody
	public Map<String,Object> getQuestionInfoList(HttpServletRequest req){
		Map<String,Object> map = new HashMap<String,Object>();
	 	String currentPage = req.getParameter("offset");
        String pageSize = req.getParameter("limit");
        String content = req.getParameter("content");
		try{
			Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("startPage", Integer.parseInt(currentPage));
            paramMap.put("pageSize", Integer.parseInt(pageSize));
            paramMap.put("content", content);
            map = questionService.getQuestionInfoList(paramMap);
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return map;
	}
	
	/**
	 * 获取问答的照片
	 * @param req
	 * @return
	 */
	@RequestMapping("liveManager/getQuestionInfoImg")
	@ResponseBody
	public Map<String,Object> getActivityInfoImg(HttpServletRequest req){
		Map<String,Object> map = new HashMap<String,Object>();
		Long questionId = Long.parseLong(req.getParameter("questionId"));
		List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
		try {
			Question selectByPrimaryKey = questionService.selectByPrimaryKey(questionId);
			String imgPost = selectByPrimaryKey.getContentImg();
			String[] imgList = imgPost.split(",");
			System.err.println(imgList.length);
			String url = "http://" + req.getServerName() + ":"+ req.getServerPort()+ req.getContextPath()+"/";
			for(int i = 0;i<imgList.length;i++) {
				Map<String,Object> map2 = new HashMap<String,Object>();
				map2.put("alt", "问答图片-"+(i+1));
				map2.put("pid", i);
				map2.put("src", url+"common/getGoodsPhoto?photoUrl="+imgList[i]);
				map2.put("thumb", url+"common/getGoodsPhoto?photoUrl="+imgList[i]);
				data.add(map2);
			}
			map.put("title", "问答相册");
			map.put("id", questionId);
			map.put("start", 0);
			map.put("data", data);
		}catch(Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return map;
	}
	/**
	 * 关闭问答
	 * @param req
	 * @return
	 */
	@RequestMapping("liveManager/closeQuestion")
	@ResponseBody
	public int closequestion(HttpServletRequest req) {
		int num = 0;
		Long questionId = Long.parseLong(req.getParameter("questionId"));
		try {
			Question question = new Question();
			question.setQuestionId(questionId);
			question.setState((byte) 2);
			question.setOpTime(new Date());
			num = questionService.updateByPrimaryKeySelective(question );
		}catch(Exception e) {
			num = 0 ;
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return num ;
	}
	
	/**
	 * 打开问答
	 * @param req
	 * @return
	 */
	@RequestMapping("liveManager/openQuestion")
	@ResponseBody
	public int openquestion(HttpServletRequest req) {
		int num = 0;
		Long questionId = Long.parseLong(req.getParameter("questionId"));
		try {
			Question question = new Question();
			question.setQuestionId(questionId);
			question.setState((byte) 1);
			question.setOpTime(new Date());
			num = questionService.updateByPrimaryKeySelective(question );
		}catch(Exception e) {
			num = 0 ;
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return num ;
	}
	
	/**
	 * 置顶
	 * @param req
	 * @return
	 */
	@RequestMapping("liveManager/openQuestionIsTop")
	@ResponseBody
	public int openIsTop(HttpServletRequest req) {
		int num = 0;
		Long questionId = Long.parseLong(req.getParameter("questionId"));
		try {
			Question question = new Question();
			question.setQuestionId(questionId);
			question.setIsTop((byte) 1);
			question.setToTopTime(new Date());
			num = questionService.updateByPrimaryKeySelective(question );
		}catch(Exception e) {
			num = 0 ;
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return num ;
	}
	
	
	/**
	 * 问答取消置顶
	 * @param req
	 * @return
	 */
	@RequestMapping("liveManager/cancelQuestionIsTop")
	@ResponseBody
	public int cancelIsTop(HttpServletRequest req) {
		int num = 0;
		Long questionId = Long.parseLong(req.getParameter("questionId"));
		try {
			Question question = new Question();
			question.setQuestionId(questionId);;
			question.setIsTop((byte) 0);
			num = questionService.updateByPrimaryKeySelective(question );
		}catch(Exception e) {
			num = 0 ;
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return num ;
	}
	
	
	/**
	 * 问答加精
	 * @param req
	 * @return
	 */
	@RequestMapping("liveManager/openQuestionIsGood")
	@ResponseBody
	public int openIsGood(HttpServletRequest req) {
		int num = 0;
		Long questionId = Long.parseLong(req.getParameter("questionId"));
		try {
			Question question = new Question();
			question.setQuestionId(questionId);
			question.setIsGood((byte) 1);
			question.setIsGoodsTime(new Date());
			num = questionService.updateByPrimaryKeySelective(question );
		}catch(Exception e) {
			num = 0 ;
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return num ;
	}
	
	/**
	 * 问答取消加精
	 * @param req
	 * @return
	 */
	@RequestMapping("liveManager/cancelQuestionIsGood")
	@ResponseBody
	public int cancelIsGood(HttpServletRequest req) {
		int num = 0;
		Long questionId = Long.parseLong(req.getParameter("questionId"));
		try {
			Question question = new Question();
			question.setQuestionId(questionId);
			question.setIsGood((byte) 0);
			num = questionService.updateByPrimaryKeySelective(question );
		}catch(Exception e) {
			num = 0 ;
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return num ;
	}
	
	
	
	
	
}
